Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | export const dynamic = "force-dynamic"; import { NextRequest, NextResponse } from 'next/server'; import { } from "next-auth"; import { prisma } from "@/lib/prisma"; import { z } from "zod"; import { withAdmin, withErrorHandling, successResponse, ApiError, ApiSuccessResponse, ApiErrorResponse } from "@/lib/api"; import { RouteContext } from "@/lib/api/middleware"; const UpdateTierSchema = z.object({ name: z.string().min(1).optional(), minPoints: z.number().int().min(0).optional(), pointsMultiplier: z.number().min(0).optional(), perks: z.array(z.string()).optional(), description: z.string().optional() }); interface RouteParams { params: Promise<{ id: string }>; } /** * GET /api/admin/loyalty/tiers/[id] * Get tier details */ async function handleGet( request: NextRequest, context: RouteContext | undefined ): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> { const { id } = await (context as RouteParams).params; const tierId = parseInt(id); if (isNaN(tierId)) { throw ApiError.badRequest("Invalid tier ID"); } const tier = await prisma.loyaltyTier.findUnique({ where: { id: tierId }, include: { program: true } }); if (!tier) { throw ApiError.notFound("Tier"); } // Get member count for this tier const memberCount = await prisma.customerLoyalty.count({ where: { currentTierId: tierId } }); return successResponse({ ...tier, memberCount }); } /** * PATCH /api/admin/loyalty/tiers/[id] * Update tier */ async function handlePatch( request: NextRequest, context: RouteContext | undefined ): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> { const { id } = await (context as RouteParams).params; const tierId = parseInt(id); if (isNaN(tierId)) { throw ApiError.badRequest("Invalid tier ID"); } const body = await request.json(); const validationResult = UpdateTierSchema.safeParse(body); if (!validationResult.success) { throw ApiError.validation("Invalid data", validationResult.error.issues); } const validatedData = validationResult.data; const tier = await prisma.loyaltyTier.findUnique({ where: { id: tierId } }); if (!tier) { throw ApiError.notFound("Tier"); } const updatedTier = await prisma.loyaltyTier.update({ where: { id: tierId }, data: { name: validatedData.name, minPoints: validatedData.minPoints, pointsMultiplier: validatedData.pointsMultiplier, perks: validatedData.perks } }); return successResponse(updatedTier); } /** * DELETE /api/admin/loyalty/tiers/[id] * Delete tier (only if no members) */ async function handleDelete( request: NextRequest, context: RouteContext | undefined ): Promise<NextResponse<ApiSuccessResponse<{ message: string }> | ApiErrorResponse>> { const { id } = await (context as RouteParams).params; const tierId = parseInt(id); if (isNaN(tierId)) { throw ApiError.badRequest("Invalid tier ID"); } const tier = await prisma.loyaltyTier.findUnique({ where: { id: tierId } }); if (!tier) { throw ApiError.notFound("Tier"); } // Check if any members have this tier const memberCount = await prisma.customerLoyalty.count({ where: { currentTierId: tierId } }); if (memberCount > 0) { throw ApiError.badRequest(`Cannot delete tier with ${memberCount} active members`); } await prisma.loyaltyTier.delete({ where: { id: tierId } }); return successResponse({ message: "Tier deleted successfully" }); } export const GET = withErrorHandling(withAdmin(handleGet)); export const PATCH = withErrorHandling(withAdmin(handlePatch)); export const DELETE = withErrorHandling(withAdmin(handleDelete)); |